/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 *
 * @author SONY
 */
public class UserDAO {
    
    DataSource ds;
    
    public UserDAO() {
        try {
            Context context = new InitialContext();
            ds = (DataSource) context.lookup("java:comp/env/jdbc/FashionShopDatasource");
        } catch (Exception e) {
            System.err.println(e);
        }
    }
    
    public User search(String login_email, String login_password) {
        try {
            Connection conn = ds.getConnection();
            String sql = "SELECT * FROM User_Login WHERE EmailAddress=? AND Password=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, login_email);
            pstmt.setString(2, login_password);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int memberID = rs.getInt("MemberID");
                User user = new User(login_email, login_password, memberID);
                return user;
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        return null;
    }
    
    public boolean searchEmail(String email) {
        try {
            Connection conn = ds.getConnection();
            String sql = "SELECT * FROM User_Login WHERE EmailAddress=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setNString(1, email);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int memberID = rs.getInt("MemberID");
                String password = rs.getString("Password");
                User user = new User(email, password, memberID);
                if(user != null) {
                    return true;
                }
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }
    
    public boolean insert (String email, String password, int memberid) {
        try {
            Connection conn = ds.getConnection();
            String sql = "INSERT INTO User_Login ([EmailAddress],[Password],[MemberID]) VALUES(?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setNString(1, email);
            pstmt.setNString(2, password);
            pstmt.setInt(3, memberid);
            if (pstmt.executeUpdate() > 0) {
                return true;
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }
    
    public boolean update_Password (String email, String password) {
        try {
            Connection conn = ds.getConnection();
            String sql = "UPDATE User_Login SET Password=? WHERE EmailAddress=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setNString(1, password);
            pstmt.setNString(2, email);
            if (pstmt.executeUpdate() > 0) {
                return true;
            }
        } catch (Exception e) {
            System.err.println(e);
        }
        return false;
    }
}
